<?php
class Wallet_manage extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		// 登录校验
		$this->common->check_login_mobile ();
		$this->load->helper ( array (
				'form',
				'url' ,
            'system'
		) );
	}
	
	// 个人中心首页
	public function index($page=0) {
        $perpage=10;

        if($page==0){
            $page=1;
        }
		$this->load->model ( 'user_model' );
		$this->load->model ( 'gongyibi_transfer_details_model' );

		// 显示自己账号
		$result = $this->user_model->get_user_info_id ( $_SESSION ['mobile'] ['id'] );
		$data ['user_account'] = $result ['user_account'];
        $data ['public_money'] = $result ['public_money'];

        // 查询自己公益币支出收入记录
        $data['details_list'] = $this->gongyibi_transfer_details_model->out_list_all($page,$perpage,$_SESSION ['user'] ['id']);
        $data['data_count']=$this->gongyibi_transfer_details_model->out_list_all_count($_SESSION ['user'] ['id']);
        $data['multi']=multi($data['data_count'],$perpage,$page,site_url('mobile/wallet_manage/index/?'),'?');


		$data ['left'] = $this->load->view ( 'mobile/left', '', TRUE );
		$data ['foot'] = $this->load->view ( 'mobile/foot', '', TRUE );

		$this->load->view ( 'mobile/wallet_manage', $data );
	}

    // 公益币转账
    public function transfer() {
        $this->load->model ( 'user_model' );
        $this->load->model ( 'gongyibi_transfer_details_model' );

        // 查看对方账号是否存在
        $target_user = $this->user_model->get_uesr_info ( $_POST ['target_user_account'] );
        if (! $target_user) {
            exit ( $this->common->alert_msg_goback ( '转账失败，对方公益账号不存在' ) );
            //echo "转账失败，对方公益账号不存在";
        }

        $me = $this->user_model->get_user_info_id ( $_SESSION ['user'] ['id'] );
        // 查看自己
        if ($me ['user_account'] == $_POST ['target_user_account']) {
            exit ( $this->common->alert_msg_goback ( '转账失败，公益不能转给自己' ) );
        }

        // 查看自己公益币数量是否充足，二级密码
        if ($me ['public_money'] < $_POST ['number']) {
            exit ( $this->common->alert_msg_goback ( '转账失败，公益币不足' ) );
            //echo "转账失败，公益币不足";
        }
        if ($me ['spwd'] != md5 ( $_POST ['spwd'] )) {
            exit ( $this->common->alert_msg_goback ( '转账失败，二级密码错误' ) );
            //echo "转账失败，二级密码错误";
        }

        // 自己数量减少
        $number = $me ['public_money'] - $_POST ['number'];
        $result = $this->user_model->update_gongyibi_num ( $_SESSION ['user'] ['id'], $number );
        if (! $result) {
            exit ( $this->common->alert_msg_goback ( '转账失败，请重试' ) );
            //echo "转账失败，请重试";
        }

        // 对方数量增加
        $number = $target_user ['public_money'] + $_POST ['number'];
        $result = $this->user_model->update_gongyibi_num ( $target_user ['id'], $number );
        if (! $result) {
            $number = $me ['public_money'] + $_POST ['number'];
            $result = $this->user_model->update_gongyibi_num ( $_SESSION ['user'] ['id'], $number );
            exit ( $this->common->alert_msg_goback ( '转账失败，请重试' ) );
            //echo "转账失败，请重试";
        }

        $bianhao = $this->common->getnumChar ( 7 );

        // 保存两条转账记录
        $result = $this->gongyibi_transfer_details_model->transfer_details_add ( $_SESSION ['user'] ['id'], "1", $me ['user_account'], $target_user ['user_account'], $bianhao );
        if (! $result) {
            $result = $this->gongyibi_transfer_details_model->transfer_details_add ( $_SESSION ['user'] ['id'], "1", $me ['user_account'], $target_user ['user_account'], $bianhao );
        }
        $result = $this->gongyibi_transfer_details_model->transfer_details_add ( $target_user ['id'], "2", $target_user ['user_account'], $me ['user_account'], $bianhao );
        if (! $result) {
            $result = $this->gongyibi_transfer_details_model->transfer_details_add ( $target_user ['id'], "2", $target_user ['user_account'], $me ['user_account'], $bianhao );
        }

        exit ( $this->common->alert_msg_url ( '转账成功', site_url ( 'mobile/wallet_manage' ) ) );
        //echo "1";
    }
}